package top.vchao.webmusicplayer;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.webkit.JavascriptInterface;

import top.vchao.webmusicplayer.utils.LogUtils;
import top.vchao.webmusicplayer.utils.ToastUtil;


/**
 * @ 创建时间: 2017/12/7 on 11:29.
 * @ 描述：预留接口  通过js调用android原生方法
 * @ 作者: 郑卫超 QQ: 2318723605
 */

public class JavaScriptinterface {
    Activity mActivity;

    public JavaScriptinterface(Activity c) {
        mActivity = c;
    }

    /**
     * 与js交互时用到的方法，在js里直接调用的
     */
    @JavascriptInterface
    public void showToast(String content) {
        ToastUtil.showShort(mActivity, content);
    }

    @JavascriptInterface
    public void play() {
        try {
            ((WebMusicActivity) mActivity).resume();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void pause() {
        try {
            ((WebMusicActivity) mActivity).pause();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void playUrl(String name,String url) {
        try {
            ((WebMusicActivity) mActivity).play(name,url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void seekTo(String position) {
        try {
            ((WebMusicActivity) mActivity).seekTo(Integer.parseInt(position));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void getPlayInfo() {
        try {
            ((WebMusicActivity) mActivity).getPlayInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void setTimerState(boolean state) {
        LogUtils.e("------setTimerState "+state);
        try {
            ((WebMusicActivity) mActivity).setTimerState(state);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 与js交互时用到的方法，在js里直接调用的
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @JavascriptInterface
    public void selectContacts(String jsname) {
        if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            //申请权限  第二个参数是一个 数组 说明可以同时申请多个权限
            ActivityCompat.requestPermissions(mActivity, new String[]{android.Manifest.permission.READ_CONTACTS}, 1);
        } else {//已授权
            Uri uri = Uri.parse("content://contacts/people");
            Intent intent = new Intent(Intent.ACTION_PICK, uri);
            mActivity.startActivityForResult(intent, 0);
        }
    }

    /**
     * 关闭当前页面
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @JavascriptInterface
    public void closePage() {
        LogUtils.e("关闭当前页面 ");
        //打开选择,本次允许选择的数量
        mActivity.finish();
    }

}

